Le kernel Python est installé par défaut avec Jupyter.
print("Ceci est du code Python")
Ceci est du code Python
import sys
print(sys.version)
3.6.9 (default, Oct 8 2020, 12:12:24) [GCC 8.4.0]
Pour calculer la fonction $n! := 1 \times 2 \times \dots \times n$ ($n\in\mathbb{N}$), on peut penser à une solution récursive (qui coutera en espace mémoire à cause de la pile d'appel) et une solution impérative.
def fact(n: int) -> int:
# note: ces indications de type sont optionnelles, mais appréciées
""" Factorielle de n, n! = 1 * 2 * .. * n."""
if n <= 1:
return 1
else:
return n * fact(n-1)
Note : ces commentaires "docstring" entre """ trois guillements""" sont optionnels mais appécies, car cela donne une documentation à chaque fonction :
help(fact) # depuis une console Python normale
Help on function fact in module __main__: fact(n:int) -> int Factorielle de n, n! = 1 * 2 * .. * n.
# depuis IPython ou Jupyter
fact?
Signature: fact(n:int) -> int Docstring: Factorielle de n, n! = 1 * 2 * .. * n. File: ~/publis/Info-Prepas-MP2I/Modele-de-livre-avec-Jupyter-Book.git/notebooks/<ipython-input-11-4665928d2bba> Type: function
Exemples :
for i in range(1, 23):
print(f"fact({i:2}) = {fact(i)}")
fact( 1) = 1 fact( 2) = 2 fact( 3) = 6 fact( 4) = 24 fact( 5) = 120 fact( 6) = 720 fact( 7) = 5040 fact( 8) = 40320 fact( 9) = 362880 fact(10) = 3628800 fact(11) = 39916800 fact(12) = 479001600 fact(13) = 6227020800 fact(14) = 87178291200 fact(15) = 1307674368000 fact(16) = 20922789888000 fact(17) = 355687428096000 fact(18) = 6402373705728000 fact(19) = 121645100408832000 fact(20) = 2432902008176640000 fact(21) = 51090942171709440000 fact(22) = 1124000727777607680000
Et la solution impérative :
def fact_imp(n: int) -> int:
""" Factorielle de n, n! = 1 * 2 * .. * n."""
f: int = 1
for i in range(2, n+1):
f = f * i # f *= i est aussi possible
return f
for i in range(1, 23):
print(f"fact_imp({i:2}) = {fact_imp(i)}")
fact_imp( 1) = 1 fact_imp( 2) = 2 fact_imp( 3) = 6 fact_imp( 4) = 24 fact_imp( 5) = 120 fact_imp( 6) = 720 fact_imp( 7) = 5040 fact_imp( 8) = 40320 fact_imp( 9) = 362880 fact_imp(10) = 3628800 fact_imp(11) = 39916800 fact_imp(12) = 479001600 fact_imp(13) = 6227020800 fact_imp(14) = 87178291200 fact_imp(15) = 1307674368000 fact_imp(16) = 20922789888000 fact_imp(17) = 355687428096000 fact_imp(18) = 6402373705728000 fact_imp(19) = 121645100408832000 fact_imp(20) = 2432902008176640000 fact_imp(21) = 51090942171709440000 fact_imp(22) = 1124000727777607680000
%load_ext itikz
The itikz extension is already loaded. To reload it, use: %reload_ext itikz
%%itikz --file-prefix tikz-figures-from-Python- --implicit-pic
\draw[help lines] grid (5, 5);
\draw[fill=black!50] (1, 1) rectangle (2, 2);
\draw[fill=black!50] (2, 1) rectangle (3, 2);
\draw[fill=black!50] (3, 1) rectangle (4, 2);
\draw[fill=black!50] (3, 2) rectangle (4, 3);
\draw[fill=black!50] (2, 3) rectangle (3, 4);
%%itikz --file-prefix tikz-figures-from-Python- --implicit-pic --scale=0.4
\tikzstyle{vertexcover} = [circle,fill=green,draw];
\tikzstyle{matching} = [ draw=blue!55, line width=5];
\tikzstyle{matchingN} = [ draw=green!55, line width=5];
\tikzstyle{vertex} = [circle,fill=none,draw];
\node[vertex] (v2) at (0,0) {};
\node[vertexcover] (v3) at (0,3) {};
\node[vertexcover] (v4) at (2,2) {};
\node[vertexcover] (v11) at (3,0) {};
\node[vertexcover] (v5) at (2,-2) {};
\node[vertexcover] (v6) at (1,-3) {};
\node[vertexcover] (v8) at (-1,-3) {};
\node[vertexcover] (v9) at (-2,-2) {};
\node[vertexcover] (v10) at (-3,0) {};
\node[vertexcover] (v1) at (-2,2) {};
\draw (v1) edge (v2);
\draw (v3) edge (v2);
\draw (v4) edge (v2);
\draw (v5) edge (v2);
\draw (v6) edge (v2);
\draw (v8) edge (v2);
\draw (v9) edge (v2);
\draw (v2) edge (v10);
\draw (v2) edge (v11);
!ls tikz-figures-from-Python-*
tikz-figures-from-Python-b7c37e91289ef17e6c34e0d00645dabd.svg tikz-figures-from-Python-b7c37e91289ef17e6c34e0d00645dabd.tex tikz-figures-from-Python-c33720202396643581aed22f595383d4.svg tikz-figures-from-Python-c33720202396643581aed22f595383d4.tex
TODO: plus tard !